﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using RAP.Framework.Libary.DataSource;
using RAP.Framework.Libary.DataSource.Model;
using RAP.Web.Libary.Common.Model;
using RAP.Web.Libary.Controller.Attributes;
using RAP.Web.Libary.Service.Session;
using System;
using System.Threading.Tasks;

namespace RAP.Web.Libary.Controller
{
  /// <summary>
  /// 控制器基类
  /// </summary>
  public class APIControllerBase : Microsoft.AspNetCore.Mvc.Controller, IWebController
  {
    /// <summary>
    /// 
    /// </summary>
    public APIControllerBase()
    {
      NamePsace = GetType().FullName;
    }

    /// <summary>
    /// 名称空间
    /// </summary>
    public virtual string NamePsace { get; protected set; }

    /// <summary>
    /// 请求参数对象，支持Body、QueryString、Form
    /// </summary>
    public virtual IAPIQueryParams QueryParams { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public virtual IServiceProvider ServiceProvider
    {
      get
      {
        return HttpContext.RequestServices;
      }
    }

    /// <summary>
    /// Action执行完毕后的特殊处理
    /// </summary>
    /// <param name="context"></param>
    public override void OnActionExecuted(ActionExecutedContext context)
    {
      NotJSONResultAttribute.ProcessAttributes(context);
      base.OnActionExecuted(context);
    }

    public new JsonResult Json(object data, JsonSerializerSettings serializerSettings)
    {
      return new JsonResult(data, serializerSettings);
    }

    public new JsonResult Json(object data)
    {
      return new JsonResult(data);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    protected async virtual Task<JsonResult> JSONContentAsync(object data)
    {
      return await Task.FromResult(JSONContent(data));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public virtual JsonResult JSONContent(object data)
    {
      if (data.HasError())
      {
        return Json(data);
      }
      return Json(ServiceResult.CreateSuccessResult(data), JsonConvert.DefaultSettings());
    }

    /// <summary>
    /// 创建一个错误返回
    /// </summary>
    /// <param name="errorMessage"></param>
    /// <param name="errorCode"></param>
    /// <returns></returns>
    public async virtual Task<JsonResult> ErrorContent(string errorMessage, int errorCode = int.MaxValue)
    {
      return await Task.FromResult(Json(ServiceResult.CreateErrorResult(errorMessage, errorCode)));
    }

    /// <summary>
    /// 默认的会话无效结果
    /// </summary>
    /// <returns></returns>
    public async virtual Task<JsonResult> InvalidSession()
    {
      return await ErrorContent("会话无效！", 100000);
    }

    /// <summary>
    /// 创建一个错误返回
    /// </summary>
    /// <param name="errorResult"></param>
    /// <returns></returns>
    public async virtual Task<JsonResult> ErrorContent(ServiceResult errorResult)
    {
      return await Task.FromResult(Json(errorResult));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public async virtual Task<object> Test()
    {
      return await Task.FromResult(this.NamePsace);
    }
  }
}
